class Queue{
	constructor() {
		this.count = 0;               //控制队列的大小
		this.lowestCount = 0;        //追踪第一个元素
		this.items = {};            //对象存储元素
	}

	enqueue(element) {      //向队列添加元素
		this.items[this.count] = element;  //count作为对象的键
		this.count++;                       //新增后 count+1
	}

	dequeue() {             //从队列移除元素
		if(this.isEmpty()) {            //队列为空 返回undefined
			return undefined
		}
		const result = this.items[this.lowestCount];  //保存队列头部的值 方便返回
		delete this.items[this.lowestCount];        //删除队列头部的值
		this.lowestCount++;                         // +1后 lowestCount 指向下一个
		return result;
	}

	isEmpty(){              //查看队列是否为空
		// return this.count - this.lowestCount === 0;
		return this.size() === 0;
	}

	size(){                 //获取队列 长度
		return this.count - this.lowestCount;
	}

	clear(){                //清空队列
		this.items = {};
		this.count = 0;
		this.lowestCount = 0;
	}
	toString(){             //创建toString方法
		if(this.isEmpty()){
			return ''
		}
		//queue的第一个索引值不一定是0，所以用lowestCount的位置开始迭代队列
		let objString = `${this.items[this.lowestCount]}`;
		for(let i = this.lowestCount + 1; i < this.count; i++){
			objString = `${objString},${this.items[i]}`
		}
		return objString
	}
}

			//  使用queue类
const queue = new Queue();
console.log(queue.isEmpty())
queue.enqueue('John')
queue.enqueue('Tom')
queue.enqueue('James')
console.log(queue.size())
queue.dequeue('Tom')
console.log(queue.size())
console.log(queue)
console.log(queue.toString())